home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / common / hydrasocket.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  2KB  |  51 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import socket
  5. from util import lock, do
  6. from common.timeoutsocket import iptuples, TimeoutSocket
  7.  
  8. class HydraSocket(object):
  9.     
  10.     def tryconnect(self, ips, on_connect, on_fail, timeout = 2, cls = TimeoutSocket):
  11.         self._ips = iptuples(ips)
  12.         self.on_connect = on_connect
  13.         self.on_fail = on_fail
  14.         self.timetowait = timeout
  15.         self.numfails = 0
  16.         self.success = False
  17.         self.socks = [ cls(False) for __ in self._ips ]
  18.         self.connected_sock = None
  19.         [ sock.tryconnect(ip, self.on_success, self.singlefail, timeout = self.timetowait) for sock, ip in zip(self.socks, self._ips) ]
  20.  
  21.     
  22.     def on_success(self, sock):
  23.         if self.success:
  24.             sock.do_disconnect()
  25.         else:
  26.             sock.cancel_timeout()
  27.             (do,)((lambda .0: for sock_ in .0:
  28. if sock_ is not sock:
  29. sock_.do_disconnect()continue)(self.socks))
  30.             self.success = True
  31.             self.connected_sock = sock
  32.             self.on_connect(sock)
  33.  
  34.     on_success = lock(on_success)
  35.     
  36.     def singlefail(self):
  37.         self.numfails += 1
  38.         if self.numfails >= len(self._ips):
  39.             self.on_fail()
  40.         
  41.  
  42.     singlefail = lock(singlefail)
  43.     
  44.     def send(self, *a, **k):
  45.         if self.connected_sock is None:
  46.             raise socket.error('HydraSocket is not connected')
  47.         
  48.         self.connected_sock.send(*a, **k)
  49.  
  50.  
  51.